<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kubernetes配置上传</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
        }

        .upload-container {
            max-width: 600px;
            margin: 50px auto;
            padding: 30px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            display: flex; /* 新增：Flex 布局控制子元素 */
            flex-direction: column; /* 子元素垂直排列 */
            align-items: center; /* 子元素水平居中 */
            gap: 25px; /* 子元素之间的垂直间距 */
        }

        .upload-area {
            border: 2px dashed #ccc;
            border-radius: 8px;
            padding: 30px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
            width: 100%; /* 占满父容器宽度 */
        }

        .upload-area:hover {
            border-color: #0d6efd;
            background-color: #f8f9ff;
        }

        .file-info {
            display: none;
            width: 100%; /* 占满父容器宽度 */
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f8f9ff;
            border-radius: 6px;
            border-left: 4px solid #0d6efd;
        }

        .result {
            padding: 15px;
            border-radius: 8px;
            margin-top: 20px;
            display: none;
            animation: fadeIn 0.3s ease;
            width: 100%; /* 占满父容器宽度 */
        }

        .success {
            background-color: #d1e7dd;
            color: #0f5132;
            border-left: 4px solid #0d6efd;
        }

        .error {
            background-color: #f8d7da;
            color: #842029;
            border-left: 4px solid #dc3545;
        }

        /* 上传按钮专属样式 */
        .upload-btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            background: linear-gradient(135deg, #0d6efd 0%, #0b5ed7 100%);
            color: white;
            font-weight: 500;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(13, 110, 253, 0.2);
            display: inline-flex; /* 弹性布局对齐图标和文字 */
            align-items: center;
            gap: 8px;
            margin: 0 auto; /* 配合父容器 Flex 布局居中 */
        }

        .upload-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(13, 110, 253, 0.3);
            background: linear-gradient(135deg, #0b5ed7 0%, #0a51c7 100%);
        }

        .upload-btn:active {
            transform: translateY(0);
            box-shadow: 0 2px 4px rgba(13, 110, 253, 0.2);
        }

        .upload-btn:disabled {
            opacity: 0.8;
            cursor: not-allowed;
        }

        .spin-icon {
            display: none;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-10px); }
            to { opacity: 1; transform: translateY(0); }
        }
    </style>
</head>
<body>
<div class="upload-container">
    <h2 class="text-center mb-4">Kubernetes配置上传</h2>

    <!-- 上传区域 -->
    <div id="upload-area" class="upload-area">
        <i class="fas fa-cloud-upload-alt fa-3x mb-3" style="color: #0d6efd;"></i>
        <h5>拖放文件到这里或点击选择</h5>
        <p class="text-muted">支持YAML或config格式文件</p>
        <input type="file" id="file-input" accept=".yaml,.yml,.config" style="display: none;">
    </div>

    <!-- 文件信息提示 -->
    <div id="file-info" class="file-info">
        <h5>已选择文件:</h5>
        <p>文件名: <span id="file-name"></span></p>
        <p>文件大小: <span id="file-size"></span></p>
    </div>

    <!-- 结果提示 -->
    <div id="result" class="result"></div>

    <!-- 上传并验证按钮（居中） -->
    <button id="submit-btn" class="upload-btn" onclick="uploadFile()">
        <i class="fas fa-upload me-2"></i>
        <span class="btn-text">上传并验证</span>
        <i class="fas fa-spinner fa-spin spin-icon" id="spin-icon"></i>
    </button>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/js/all.min.js"></script>
<script>
    // 获取DOM元素
    const fileInput = document.getElementById('file-input');
    const uploadArea = document.getElementById('upload-area');
    const fileInfo = document.getElementById('file-info');
    const fileName = document.getElementById('file-name');
    const fileSize = document.getElementById('file-size');
    const resultDiv = document.getElementById('result');
    const submitBtn = document.getElementById('submit-btn');
    const spinIcon = document.getElementById('spin-icon');

    // 点击上传区域触发文件选择
    uploadArea.addEventListener('click', () => fileInput.click());

    // 处理文件选择
    fileInput.addEventListener('change', (e) => {
        if (e.target.files.length) {
            updateFileInfo(e.target.files[0]);
        }
    });

    // 拖拽功能
    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
        uploadArea.addEventListener(eventName, preventDefaults, false);
    });

    function preventDefaults(e) {
        e.preventDefault();
        e.stopPropagation();
    }

    ['dragenter', 'dragover'].forEach(eventName => {
        uploadArea.addEventListener(eventName, highlight, false);
    });

    ['dragleave', 'drop'].forEach(eventName => {
        uploadArea.addEventListener(eventName, unhighlight, false);
    });

    function highlight() {
        uploadArea.classList.add('highlight');
    }

    function unhighlight() {
        uploadArea.classList.remove('highlight');
    }

    uploadArea.addEventListener('drop', handleDrop, false);

    function handleDrop(e) {
        const dt = e.dataTransfer;
        const file = dt.files[0];
        fileInput.files = dt.files;
        updateFileInfo(file);
    }

    function updateFileInfo(file) {
        fileName.textContent = file.name;
        fileSize.textContent = (file.size / 1024).toFixed(2) + ' KB';
        fileInfo.style.display = 'block';
    }

    // 上传文件（核心逻辑）
    function uploadFile() {
        const file = fileInput.files[0];
        if (!file) {
            showResult('请先选择文件', false);
            return;
        }

        // 显示加载状态
        submitBtn.disabled = true;
        submitBtn.querySelector('.btn-text').textContent = '验证中...';
        spinIcon.style.display = 'inline-block';

        const formData = new FormData();
        formData.append('kubeconfig', file);

        fetch('/DoSave', {
            method: 'POST',
            body: formData
        })
            .then(response => response.json())
            .then(data => {
                showResult(data.message, data.success);
                if (data.success) {
                    setTimeout(() => window.location.href = '/xj', 3000);
                }
            })
            .catch(error => {
                showResult(`请求失败: ${error.message}`, false);
            })
            .finally(() => {
                // 恢复按钮状态
                submitBtn.disabled = false;
                submitBtn.querySelector('.btn-text').textContent = '上传并验证';
                spinIcon.style.display = 'none';
            });
    }

    // 统一结果提示函数
    function showResult(message, isSuccess) {
        resultDiv.style.display = 'block';
        resultDiv.className = `result ${isSuccess ? 'success' : 'error'}`;
        resultDiv.innerHTML = `
            <div class="d-flex align-items-center">
                <i class="fas ${isSuccess ? 'fa-check-circle' : 'fa-times-circle'} me-2"></i>
                <span>${message}</span>
            </div>
        `;
    }
</script>
</body>
</html>